package com.ruoyi.web.controller.useres;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.UserToTexts.domain.UserText;
import com.ruoyi.UserToTexts.service.IUserTextService;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.system.domain.UserTextClass;
import com.ruoyi.system.service.IUserTextClassService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.useres.domain.User;
import com.ruoyi.useres.service.IUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 用户Controller
 * 
 * @author ruoyi
 * @date 2024-11-24
 */
@RestController
@RequestMapping("/useres/euser")
public class UserController extends BaseController
{
    @Autowired
    private IUserTextClassService userTextClassService;
    @Autowired
    private SysLoginService loginService;
    @Autowired
    private IUserTextService userTextService;
    @Autowired
    private IUserService userService;

    /**
     * 查询用户列表
     */
//    @PreAuthorize("@ss.hasPermi('useres:euser:list')")
    @GetMapping("/list")
    public TableDataInfo list(User user)
    {
        startPage();
        List<User> list = userService.selectUserList(user);
        return getDataTable(list);
    }


    /**
     * 导出用户列表
     */
    @PreAuthorize("@ss.hasPermi('useres:euser:export')")
    @Log(title = "用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, User user)
    {
        List<User> list = userService.selectUserList(user);
        ExcelUtil<User> util = new ExcelUtil<User>(User.class);
        util.exportExcel(response, list, "用户数据");
    }

    /**
     * 获取用户详细信息
     */
    @PreAuthorize("@ss.hasPermi('useres:euser:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(userService.selectUserById(id));
    }

    /**
     * 新增用户
     */
    @PreAuthorize("@ss.hasPermi('useres:euser:add')")
    @Log(title = "用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody User user)
    {
        return toAjax(userService.insertUser(user));
    }

    /**
     * 修改用户
     */
    @PreAuthorize("@ss.hasPermi('useres:euser:edit')")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody User user)
    {
        return toAjax(userService.updateUser(user));
    }

    /**
     * 删除用户
     */
    @PreAuthorize("@ss.hasPermi('useres:euser:remove')")
    @Log(title = "用户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(userService.deleteUserByIds(ids));
    }

    @GetMapping("/listByClassId/{id}")
    public AjaxResult selectAllByClassId(@PathVariable Long id){
        return success(userService.selectAllByClassId(id));
    }

//    在用户里加试卷
    @GetMapping("/addText")
    public AjaxResult addText(Long textId,Long classId){
        UserTextClass userTextClass = new UserTextClass();
        userTextClass.setClassId(classId);
        userTextClass.setTextId(textId);
        userTextClass.setUserId(SecurityUtils.getUserId());
        return success(userTextClassService.insertUserTextClass(userTextClass));
    }

//

//    用户登录
//    PostMapping("/login")
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody){
        System.err.println(loginBody);
        User user = userService.selectUserByUserName(loginBody.getUsername());
//        AjaxResult ajax = AjaxResult.success();
//        // 生成令牌
//        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
//                loginBody.getUuid());
//        ajax.put(Constants.TOKEN, token);
//        System.err.println(ajax);
        return success(user);
//        return userService.login(user);
//        return null;
    }

    @GetMapping("/my")
    public AjaxResult getMyInfo(){
        return success(userService.selectUserById(SecurityUtils.getUserId()));
    }

    @GetMapping("/teacherList")
    public AjaxResult getTeacherList(){
        User user = new User();
        user.setType("老师");
        return success(userService.selectUserList(user));
    }
}
